package utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.google.gson.Gson;

public class Utils {

	public static String toJSON(Object obj) {
		Gson gson = new Gson();
		return gson.toJson(obj);
	}

	// not sure in correctness
	public static String MD5(String raw) {
		String output = null;
		try {
			MessageDigest md;
			md = MessageDigest.getInstance("MD5");
			md.update(raw.getBytes(), 0, raw.length());
			output = new BigInteger(1, md.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return output;
	}

	public static String SHA256(String data) {
		String out = null;
		try {
			MessageDigest md;
			md = MessageDigest.getInstance("SHA-256");
			md.update(data.getBytes());
			out = bytesToHex(md.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return out;
	}

	public static String bytesToHex(byte[] bytes) {
		StringBuffer result = new StringBuffer();
		for (byte byt : bytes)
			result.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1));
		return result.toString();
	}

}
